<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 会员类
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: member.php 271 2012-06-25 08:34:46Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Member
{

    const NORMAL =  0;  // 正常
    const LOCKED =  1;  // 已锁定
    const NOTACT = -1;  // 未激活

    /**
     * 获取会员信息
     *
     * @param  string  $value
     * @return FALSE|array
     */
    public static function get($value)
    {
        if (is_numeric($value))
        {
            $field = 'member_id';
        }
        elseif (Valid::email($value))
        {
            $field = 'email';
        }
        elseif (UTF8::strlen($value) > 1)
        {
            $field = 'nickname';
        }
        else
        {
            return false;
        }

        return DB::select()
            ->from('members')
            ->where($field, '=', $value)
            ->fetch_row();
    }

    /**
     * 新会员注册
     *
     * @param  array  $data
     * @return int
     * @throws Member_Exception
     */
    public static function register(array $data)
    {
        $email = Arr::get($data, 'email');

        if (empty($email))
        {
            throw new Member_Exception('Email 地址不能为空');
        }

        if ( ! Valid::email($email))
        {
            throw new Member_Exception('无效的 Email 地址');
        }

        if (self::get($email))
        {
            throw new Member_Exception('输入的 Email 地址已经被注册');
        }

        $nickname = Arr::get($data, 'nickname');
        if (empty($nickname))
        {
            throw new Member_Exception('昵称不能为空');
        }

        if ( ! preg_match('/^[^\'\"\:;,\<\>\?\/\\\*\=\+\{\}\[\]\)\(\^%\$#\!`\s]+$/', $nickname))
        {
            throw new Member_Exception('昵称不允许包含标点符号及特殊字符');
        }

        if (UTF8::strlen($nickname) < 2)
        {
            throw new Member_Exception('昵称不能少于 2 个字符');
        }

        if (self::get($nickname))
        {
            throw new Member_Exception('输入的昵称已经存在');
        }

        $password = Arr::get($data, 'password');
        if (empty($password))
        {
            throw new Member_Exception('登录密码不能为空');
        }

        if (UTF8::strlen($password) < 6)
        {
            throw new Member_Exception('登录密码不能少于 6 个字符');
        }

        $confirm = Arr::get($data, 'confirm');
        if (empty($confirm))
        {
            throw new Member_Exception('确认密码不能为空');
        }

        if ($password !== $confirm)
        {
            throw new Member_Exception('两次输入的密码不一致');
        }

        // 写入会员信息
        $member = array(
            'email' => $email,
            'nickname' => $nickname,
            'password' => Password::create($password),
            'reg_time' => time(),
            'reg_ip' => Request::$client_ip,
            'active_token' => md5(time().uniqid().Request::$user_agent),
            'status' => Member::NOTACT,
        );
        $result = DB::insert('members', $member)->execute();

        return $result[0];
    }

    /**
     * 修改个人密码
     *
     * @param  int    $member_id
     * @param  array  $data
     * @throws Member_Exception
     */
    public static function password($member_id, array $data)
    {
        if ( ! isset($data['password']) OR empty($data['password']))
        {
            throw new Member_Exception('新密码不能为空');
        }

        if (UTF8::strlen($data['password']) < 6)
        {
            throw new Member_Exception('新密码不能少于 6 个字符');
        }

        if ( ! isset($data['confirm']) OR empty($data['confirm']))
        {
            throw new Member_Exception('确认密码不能为空');
        }

        if (trim($data['password']) !== trim($data['confirm']))
        {
            throw new Member_Exception('两次输入的密码不一致');
        }

        $member = self::get($member_id);

        if (isset($data['oldpwd']))
        {
            if (empty($data['oldpwd']))
            {
                throw new Member_Exception('原始密码不能为空');
            }
            if ( ! Password::match($member['password'], $data['oldpwd']))
            {
                throw new Member_Exception('输入的原始密码不正确');
            }
        }

        // 更新密码
        DB::update('members', array(
                'password' => Password::create($data['password']))
            )
            ->where('member_id', '=', $member_id)
            ->execute();
    }

    /**
     * 获取会员状态文字描述
     *
     * @param  int  $status
     * @return string
     */
    public static function status($status, $color = TRUE)
    {
        $data = array(
            self::NORMAL => $color ? '<font color=black>正常</font>' : '正常',
            self::LOCKED => $color ? '<font color=red>已锁定</font>' : '已锁定',
            self::NOTACT => $color ? '<font color=orange>未激活</font>' : '未激活',
        );
        return isset($data[$status]) ? $data[$status] : ($color ? '<font color=gray>未知</font>' : '未知');
    }

}